<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工厂名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工厂名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vqms:factory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vqms:factory:delete']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="factoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="父工厂名称" align="center" prop="parentName" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="工厂名称" align="center" prop="name" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="是否需要排队" align="center" prop="isQueue" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.isQueue === 0" style="color: red;">否</span>
          <span v-if="scope.row.isQueue === 1" style="color: green;">是</span>
        </template>
      </el-table-column>
      <el-table-column label="经度" align="center" prop="longitude" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="纬度" align="center" prop="latitude" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="电子围栏（千米）" align="center" prop="electronicFence" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.qrcodeUrlIn != null"
            style="color:orange;font-size:16px"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleQrcode(scope.row)"
            v-hasPermi="['vqms:factory:query']"
          >项目码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vqms:factory:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vqms:factory:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 工厂码对话框-->
    <el-dialog :title="title" :visible.sync="open2" width="800px" append-to-body>
      <el-row>
        <el-col :span="12" >
          <el-image style="width: 350px; height: 350px" :src="this.form.qrcodeUrlIn"></el-image>
        </el-col>
        <el-col :span="12" >
          <el-image style="width: 350px; height: 350px" :src="this.form.qrcodeUrlOut"></el-image>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 添加或修改工厂对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12" >
            <el-form-item label="父工厂" prop="parentName">
              <el-autocomplete
                clearable
                style="width:100%;"
                v-model="form.parentName"
                value-key="name"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入父工厂"
                @select="handleSelectFactory">
              </el-autocomplete>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电子围栏" prop="electronicFence">
              <el-input v-model="form.electronicFence" placeholder="请输入电子围栏" >
                <template slot="append">千米</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否需要排队">
              <el-select v-model="form.isQueue" placeholder="请选择是否需要排队" style="width :240px">
                <el-option v-for="isQueue in isQueueList" :key="isQueue.value" :label="isQueue.label" :value="isQueue.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectFactoryList } from "@/api/vqms/factory";
import { listFactory,addFactory,getFactory,updateFactory,delFactory } from "@/api/vqms/factory";


export default {
  //name: "Factory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工厂表格数据
      factoryList: [],
      // 是否需要排队
      isQueueList :[
        {
          label:"否",
          value:0
        },
        {
          label:"是",
          value:1
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        longitude: [
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,6})?$/,message: "只能输入六位小数以内的金额",trigger: "change"}
        ],
        latitude: [
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,6})?$/,message: "只能输入六位小数以内的金额",trigger: "change"}
        ],
        electronicFence: [
          { pattern: /^(([1-9]{1}\d*)|(0{1}))?$/,message: "只能输入整数",trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工厂列表 */
    getList() {
      this.loading = true;
      listFactory(this.queryParams).then(response => {
        this.factoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 项目码按钮操作 */
    async handleQrcode(row) {
      this.reset();
      this.open2=true;
      this.form.qrcodeUrlIn=row.qrcodeUrlIn
      this.form.qrcodeUrlOut=row.qrcodeUrlOut
      this.title = "查看项目码";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$set(this.form, "isQueue", 1);
      this.open = true;
      this.title = "添加工厂";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getFactory(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工厂";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateFactory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delFactory(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //所属工厂异步搜索
    async querySearchAsync(queryString, cb) {
      this.factoryList=[];
      let query={
        name : queryString
      }
      await selectFactoryList(query).then(response => {
        this.factoryList = response.data
      });
      var results = queryString ? this.factoryList.filter(this.createStateFilter(queryString)) : this.factoryList;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.name.indexOf(queryString) >= 0);
      };
    },
    handleSelectFactory(item) {
      this.form.parentName=item.name
      this.form.parentId=item.id
    },
  }
};
</script>
